﻿using System;

// XNA lib
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

//Oak Engine Lib
using OakEngine.GameState;
using OakEngine.Input;
using OakEngine.Camera;
using Microsoft.Xna.Framework.Input;
using MainJumpFleet.TempObjects;
using JumpFleetGameLib.UI;
// AI Setup
using MainJumpFleet.SteerBehaviors;
using OakEngine.AI.Kinematic;
using OakEngine.AI.Steering;


namespace MainJumpFleet.GameStates
{
    public class NewCameraTestScreenState : OAK_IGameState
    {
        SpriteBatch batch;
        SpriteFont mainMenuFont;
        Texture2D background;
        Texture2D dot;
        Texture2D missel;
        HUD hud;
        //
        Oak_Camera cam;
        int viewPortWidth;
        int viewportHeight;

        Vector2[] backgroundPosition;
        Vector2 dotPosition = Vector2.Zero;


        /*******************************
         * AI Section
         * 
         *******************************/
        TempObjects.EnemyShip[] enemy;

        Random r;
        

        public NewCameraTestScreenState()
        {
            Enter();
        }


        public void Enter()
        {
            mainMenuFont = CGame.CGame.GetInstance().Content.Load<SpriteFont>("Fonts\\mainMenuFont");
            batch = CGame.CGame.GetInstance().SpriteBatch;

            background = CGame.CGame.GetInstance().Content.Load<Texture2D>("textures\\starfield_1024x1024_outline");
            background = CGame.CGame.GetInstance().Content.Load<Texture2D>("textures\\starfield_1024x1024_outline");
            dot = CGame.CGame.GetInstance().Content.Load<Texture2D>("Debug\\dot");
            missel = CGame.CGame.GetInstance().Content.Load<Texture2D>("textures\\missile_6x14");
            hud = new HUD( CGame.CGame.GetInstance().Content);

            cam = new Oak_Camera(Vector2.Zero, 1024, 768, 3000, 3000,100.0f);

            viewPortWidth = CGame.CGame.GetInstance().Device.Viewport.Width;
            viewportHeight = CGame.CGame.GetInstance().Device.Viewport.Height;


            backgroundPosition = new Vector2[5];
            r = new Random();

            for (int i = 0; i < 5; i++)
            {
                backgroundPosition[i] = new Vector2(1024*i,0);
            }

            enemy = new TempObjects.EnemyShip[1000];

            for (int i = 0; i < enemy.Length; i++)
            {
                enemy[i] = new TempObjects.EnemyShip(1, dot,missel, new Vector2((float)r.Next(0,5000),(float)r.Next(0, 5000)), dotPosition,ref cam);
                
            }

        }

        public bool Input(OAK_InputManager input)
        {

            if (input.Pressed(Keys.Escape) || input.Pressed(Buttons.Back, 0))
                CGame.CGame.GetInstance().ChangeState(new MainMenuState());



            PlayerInput(ref input);

            return false;
        }
        public void Update(GameTime dt)
        {
            cam.Update(dt);

            hud.UpdateHUD(dt);

            for (int i = 0; i < enemy.Length; i++)
            {
                enemy[i].SetTarget(dotPosition);
                enemy[i].Update(dt);
            }

        }

        public void Render()
        {
            //batch.DrawString(mainMenuFont, "Welcome to Input Game Screen State\n Press Esc to go back to Main menu ", new Vector2(300,250), Color.White);

            for (int i = 0; i < 5; i++)
            {
                batch.Draw(background, backgroundPosition[i] - cam.CameraPosition, Color.White);
            }

            batch.Draw(dot, dotPosition - cam.CameraPosition, Color.Red);

            for (int i = 0; i < enemy.Length; i++)
            {
                enemy[i].Render(batch, ref cam);
                
            }

            hud.DrawHUD(batch);
        }

        public void PlayerInput(ref OAK_InputManager input)
        {
            if (input.HeldDown(Keys.D))
            {
                dotPosition.X += 5.0f;

                if (dotPosition.X > viewPortWidth / 2)
                    cam.CameraPosition += new Vector2(5.0f, 0);                
            }

            if(input.HeldDown(Keys.A))
            {
                dotPosition.X -= 5.0f;

                if (dotPosition.X >= viewPortWidth / 2)
                    cam.CameraPosition -= new Vector2(5.0f, 0);           
            }

            if (input.HeldDown(Keys.W))
            {
                dotPosition.Y -= 5.0f;

                if (dotPosition.Y >= viewportHeight / 2)
                    cam.CameraPosition -= new Vector2(0.0f, 5.0f);
            }

            if (input.HeldDown(Keys.S))
            {
                dotPosition.Y += 5.0f;

                if (dotPosition.Y >= viewportHeight / 2)
                    cam.CameraPosition += new Vector2(0.0f, 5.0f);
            }


        }

        public void Exit()
        {
        }
    }
}
